const http = require('http');
const fs = require('fs');
const path = require('path');

// 创建服务器
let server = http.createServer(function(req,res){
    console.log("服务器处理用户的请求");
    console.log(req.url);
    let url = req.url;
    if(url == "/"){
        url = "/index.html";
    }
    url = new URL("http://localhost:3000"+url);
    if(url.pathname == "/login"){
        // 登录验证的操作
        if(url.searchParams.get("username") == "aaaaaa"
            && url.searchParams.get("pwd") == "111111"){
                res.write("success");
        }else{
            res.write("fail");
        }
        res.end();
    }else{
        // 静态资源的获取
        fs.readFile(path.join('./public',url.pathname),function(err,data){
            if(err){
                res.end();
                return;
            }
            res.write(data);
            res.end();
        });
    }

   
});
// 启动服务器并打开监听端口
server.listen(3000);
console.log("服务器启动成功");